package com.moleq.logic;

import com.moleq.models.GlobalInfo;
import com.moleq.models.VdrCoupon;
import com.moleq.util.StringUtils;

public class UPC {

	public final static int MGPOS_RULE_UPC = -1;
	public final static int UNKNOW = 0;
	public final static int UPCE = 1;
	public final static int UPCA = 2;
	public final static int EAN8 = 3;
	public final static int EAN13 = 4;
	public final static int INPUT_BY_NONE = 10;
	public final static int INPUT_BY_SACNNER = 11;
	public final static int INPUT_BY_MANUAL = 12;

	public String code = "";
	public int type = UNKNOW;
	private int mMaxCodeLen = 12;
	private String mOriginalCode = "";
	public boolean IsPackageItem = false;
	public String PackageItemPrice = "";
	public int UPCInputType = INPUT_BY_NONE;
	public int PackageItemPriceDigit = GlobalInfo.getInstance().priceDigits;
	public boolean isNormalize = false;
	public boolean IsVdrCoupon = false;
	public String vdr_code,fam_code,vdr_coupon_code_no;

	public UPC(String code, int inputType,boolean isNormalize) {
		
		UPCInputType = inputType;
		mOriginalCode = code;
		this.isNormalize = isNormalize;

		switch (inputType) {
		case INPUT_BY_NONE:
			this.code = mOriginalCode;
			break;

		default:
			parseUPC();
			break;
		}
		
	}

	// public boolean checkPackageItem(String upc,int p4Digits) {
	// int iLength,itemNoIdx ;
	//
	// iLength = upc.length();
	//
	// if(isPackageItem(upc)){
	//
	// }
	//
	// if (iLength == 12 && upc.startsWith("02") ) {
	// itemNoIdx = 7;
	//
	// PackageItemPrice = upc.substring(7, 12);
	// PackageItemPrice = PackageItemPrice.substring(PackageItemPrice.length() -
	// p4Digits, p4Digits);
	//
	// // double pkgprice = Double.valueOf(upc.substring(priceIdx, 12)) ;
	// // pkgprice = BigDecmialTool.div(pkgprice, 100);
	//
	// upc = upc.substring(0, itemNoIdx) ;
	// upc = StringUtils.fillStringOnRight(upc, 12 -itemNoIdx, "0") ;
	//
	// IsPackageItem = true;
	// }
	//
	// return IsPackageItem;
	//
	// }

	private boolean isPackageItem(String upc) {
		boolean result = false;
		int iLength = upc.length();
		if (iLength == 12 && upc.startsWith("02"))
			result = true;
		return result;
	}
	
	private boolean isVdrCoupon(String upc){
		boolean result = false;
		int iLength = upc.length();
		if ((iLength == 12 && upc.startsWith("05")) || (iLength == 11 && upc.startsWith("5")) || (iLength == 12 && upc.startsWith("5")))
			result = true;
		return result;
	}

	private void parseUPC() {
	
		IsPackageItem = false;
		IsVdrCoupon = false;
		
		if (isVdrCoupon(mOriginalCode)) {
			IsVdrCoupon  = true;
			code = mOriginalCode;
			parseVdrCoupon(code);
			return;
		}
		
		code = getKeyCode(mOriginalCode);
		
		if (isPackageItem(code)) {
			IsPackageItem = true;
			parsePackageItem(code, PackageItemPriceDigit);
			
//			code = getKeyCode(code);
		
		}
	
	}

	private void parseVdrCoupon(String code) {
		int idx = code.indexOf("5");
		vdr_code = code.substring(idx + 1, idx + 6);
		idx = idx +6;
		fam_code = code.substring(idx, idx + 3);
		idx = idx +3;
		vdr_coupon_code_no = code.substring(idx, idx + 2);
	}

	private void parsePackageItem(String upc, int p4Digits) {

		int itemNoIdx;
		if (upc.length() != 12)
			return;

		
		itemNoIdx = 7;
		PackageItemPrice = upc.substring(7, 12);
		PackageItemPrice = PackageItemPrice.substring( PackageItemPrice.length() - p4Digits);

		// double pkgprice = Double.valueOf(upc.substring(priceIdx, 12)) ;
		// pkgprice = BigDecmialTool.div(pkgprice, 100);

		code = upc.substring(0, itemNoIdx);
		code = StringUtils.fillStringOnRight(code, 12 - itemNoIdx, "0");
	}

	private String getKeyCode(String upc) {
		String result = "";

		result = upc;
		int len = upc.length();
		switch (len) {
		 case 1:
		 case 2:
		 case 3:
		 case 4:
		 case 5:
		 case 7:
		 case 9:
		 case 10:
			 type = UNKNOW;
			 result = StringUtils.fillStringOnLeft(mOriginalCode, mMaxCodeLen - len, "0");
		 break;
		case 6: // UPCE
			type = UPCE;
			
			if (isNormalize) {
				result = this.ConvertUpce2Ean13(upc);
				result = this.getKeyCode(result);
			}else{
				result = StringUtils.fillStringOnLeft(mOriginalCode, mMaxCodeLen - len, "0");
			}
			
			break;
		case 8: // EAN8
			type = EAN8;
			result = this.ConvertEan8ToEan13(upc);
			result = this.getKeyCode(result);
			break;
		case 11: // UPCA
			type = UPCA;
			result = StringUtils.fillStringOnLeft(upc, 1, "0");
			break;
		case 12:
			type = MGPOS_RULE_UPC;
//			result = StringUtils.fillStringOnLeft(upc, 1, "0");
//			result = result.substring(0, 12);
//			result = upc;
			break;
		case 13: // EAN13
			type = EAN13;
			result = upc.substring(0, 12);
			break;
	
		}

		return result;
	}

	public String ConvertUpce2Upca(String upce) {
		String upca, end;

		int len = upce.length();
		if (len != 6)
			return "";

		end = upce.substring(len - 1);
		if (end == "0" || end == "1" || end == "2") {
			upca = upce.substring(0, 2) + end + "0000" + upce.substring(2, 5);
		} else if (end == "3") {
			upca = upce.substring(0, 3) + "00000" + upce.substring(3, 5);
		} else if (end == "4") {
			upca = upce.substring(0, 4) + "00000" + upce.substring(4, 5);
		} else {
			upca = upce.substring(0, 5) + "0000" + end;
		}

		return upca;

	}

	public String ConvertEan8ToEan13(String ean8) {
		String ean13 = "";

		int len = ean8.length();
		if (len != 8)
			return ean13;

		ean13 = "00000" + ean8;

		return ean13;
	}

	public String ConvertUpca2Ean13(String upca) {
		String result = "";
		result = StringUtils.fillStringOnLeft(upca, 1, "0");
		return result;
	}

	public String ConvertUpce2Ean13(String upce) {
		String result = "";
		result = this.ConvertUpce2Upca(upce);
		result = this.ConvertUpca2Ean13(result);
		return result;
	}

}
